package io.renren.modules.project.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.RandomId;
import io.renren.modules.oss.service.FileUpLoadSrv;
import io.renren.modules.project.entity.ConferenceAccessoryEntity;
import io.renren.modules.project.entity.ContactAccessoryEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.project.entity.ContactSheetEntity;
import io.renren.modules.project.service.ContactSheetService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-10-05 18:23:25
 */
@RestController
@RequestMapping("/generator/contactsheet")
public class ContactSheetController {
    @Autowired
    private ContactSheetService contactSheetService;
    @Autowired
    private FileUpLoadSrv fileUpLoadSrv;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,HttpServletRequest request) {
        //查询列表数据
        Query query = new Query(params);

        List<ContactSheetEntity> contactSheetList = contactSheetService.queryList(query,request);
        int total = contactSheetService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(contactSheetList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:contactsheet:info")
    public R info(@PathVariable("id") String id) {
        ContactSheetEntity contactSheet = contactSheetService.queryObject(id);

        return R.ok().put("contactSheet", contactSheet);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:contactsheet:save")
    public R save(@RequestBody ContactSheetEntity contactSheet) {
        contactSheetService.save(contactSheet);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:contactsheet:update")
    public R update(@RequestBody ContactSheetEntity contactSheet) {
        contactSheetService.update(contactSheet);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:contactsheet:delete")
    public R delete(@RequestBody String[] ids) {
        contactSheetService.deleteBatch(ids);

        return R.ok();
    }

    //新增联系沟通单
    @SysLog("新增联系沟通单")
    @RequestMapping("/addConferencesheet")
    public R addConferencesheet(@RequestBody ContactSheetEntity contactSheet) {
        contactSheetService.addConferencesheet(contactSheet);
        return R.ok();
    }

    //新增联系沟通单附件 uploadConferencesheet
    @SysLog("新增联系沟通单附件")
    @RequestMapping("/uploadConferencesheet")
    public R uploadConferencesheet(@RequestParam("file") MultipartFile file, ContactAccessoryEntity contactAccessoryEntity, HttpServletRequest request) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        String fileName = file.getOriginalFilename();    // 文件名
        Map<String, String> map = new HashMap<String, String>();
        try {
            map.putAll(fileUpLoadSrv.write(file, ""));
            //附件
            if ("1".equals(contactAccessoryEntity.getSheetAccessory())) {
                contactAccessoryEntity.setId(RandomId.createId() + "");
                contactAccessoryEntity.setSheetAccessory(fileName);
                contactAccessoryEntity.setAccessoryUrl(map.get("filePath"));
                contactSheetService.uploadConferencesheet(contactAccessoryEntity);
            } else {
                //回复附件
                Date currentTime = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(currentTime);
                contactAccessoryEntity.setReplyAccessory(fileName);
                contactAccessoryEntity.setReplyAccessoryUrl(map.get("filePath"));
                contactAccessoryEntity.setCreationTime(dateString);
                contactSheetService.uploadReplyAccessory(contactAccessoryEntity);
            }
            //conferenceAccessoryService.save(conferenceAccessoryEntity);
            return R.ok().put("success", "上传成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return R.error().put("error", e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            return R.error().put("error", e.getMessage());
        }
    }

}
